@echo off
chcp 65001

echo ========================================
echo Multi-Container Experiment System Startup Script
echo ========================================

REM Check if parameter exists
if "%1"=="test" (
    echo Running unit test mode...
    goto :run_tests
) else (
    echo Running multi-container startup mode...
    goto :run_multi
)

:run_tests
REM Check if Node.js is installed
node --version >nul 2>&1
if %errorlevel% neq 0 (
    echo Error: Node.js not found, please install Node.js first
    pause
    exit /b 1
)

REM Check if npm is available
npm --version >nul 2>&1
if %errorlevel% neq 0 (
    echo Error: npm not available
    pause
    exit /b 1
)

REM Install test dependencies (if not exist)
echo Checking test dependencies...
if not exist "node_modules\jest" (
    echo Installing test dependencies...
    npm install --save-dev jest @testing-library/react @testing-library/jest-dom jest-environment-jsdom @types/jest
    if %errorlevel% neq 0 (
        echo Error: Failed to install test dependencies
        pause
        exit /b 1
    )
)

REM Run tests
echo Running unit tests...
npm test

if %errorlevel% equ 0 (
    echo ========================================
    echo Tests passed!
    echo ========================================
) else (
    echo ========================================
    echo Tests failed!
    echo ========================================
)

pause
exit /b

:run_multi
REM Check if Docker is installed
docker --version >nul 2>nul
if errorlevel 1 (
    echo Please install Docker Desktop first: https://www.docker.com/products/docker-desktop
    pause
    exit /b
)

REM Check if Docker Compose is available
docker-compose --version >nul 2>nul
if errorlevel 1 (
    echo Please ensure Docker Compose is installed
    pause
    exit /b
)

REM Stop and remove existing containers
echo Stopping existing containers...
docker-compose -f docker-compose.prod.yml down

REM Start multi-container system
echo Starting multi-container system...
docker-compose -f docker-compose.prod.yml up -d

REM Wait for services to start
echo Waiting for services to start...
timeout /t 10 /nobreak >nul

REM Check service status
echo Checking service status...
docker-compose -f docker-compose.prod.yml ps

echo ========================================
echo Multi-container system started!
echo ========================================
echo Access URLs:
echo - Frontend: http://localhost:3000
echo - Backend API: http://localhost:3001
echo - Database: localhost:5432
echo ========================================

pause 